(function() {
  getlist();
  $('body').on('change','#moren',function(){
    
  });
})();
var body = $('body');
/**
 * 获取地址列表
 */
function getlist() {
  getJson(Domain + '/mbr/doListMemberAddress.do', {}, function(res) {
    //console.log(res);
    renderTmp('#listaddress', 'listaddresstpl', res);
    renderTmp('#mraddress', 'mraddresstpl', res);
  });
}
/**
 * 删除地址
 */
function daddr(id) {
  getJson(Domain + '/mbr/doDelAddress.do', { addrId: id }, function(res) {
    console.log(res);
    if (res.data) {
      alert('删除成功！');
      getlist();
    }
  });
}
/**
 * 添加收货地址(未做)
 */

function addaddr(pid, cid, did, adds, name, phone, sta, yb, bphone) {
  var pid =$('#provice').attr('data-id');
  var cid =$('#citytpl').attr('data-id');
  var did =$('#areatpl').attr('data-id');
  var adds = $('#xqdzh').val();
  var yb = $('#yzhbm').val();
  var name = $('#shjr').val();
  var phone = $('#phone').val();
  getJson(Domain + '/mbr/doSaveAddress.do', {
    provinceId: pid,
    cityId: cid,
    districtId: did,
    address: adds,
    contactName: name,
    contactTel: phone,
    status: sta,
    postCode: yb,
  }, function(res) {
    //console.log(res);
  });
}


/**
 * 设置为默认收货地址
 */
function moren(id) {
  getJson(Domain + '/mbr/doSetDefaultAddress.do', { addrId: id }, function(res) {
    //console.log(res);
    if (res.data) {
      $('#listaddress').html(' ');
      $('#mraddress').html(' ');
      getlist();
    }
  });
}



/**
 * 省市区三级联动(all)
 */
//调用省选择列表,并设置其change事件，可动态根据省修改市选择列表
body.on('change', '#provice', function() {
  //加载市选项
  var index = this.selectedIndex;
  var id = $(this).find('option').eq(index).attr('data-val');
  $(this).attr('data-id', id);

  var val = $(this).find('option').eq(index).attr('data-txt');
  $(this).attr('data-txt', val);
  $(this).attr('data-idx', index);
  var cityData = page.addressData[index].children;
  var cityContent = '';
  for (var i = 0; i < cityData.length; i++) {
    cityContent += "<option data-val='" + cityData[i].value + "'data-txt='" + cityData[i].text + "' value='" + cityData[i].text + "' >" + cityData[i].text + "</option>";
  }

  $(this).closest('.address_sel').find('#citytpl').html(cityContent);

});

// 调用市选择列表,设置其change事件，可动态根据市修改区县选择列表
body.on('change', '#citytpl', function() {
  var index = this.selectedIndex;
  var idx1 = $(this).closest('.address_sel').find('#provice').attr('data-idx');
  //加载区选项
  var id = $(this).find('option').eq(index).attr('data-val');
  $(this).attr('data-id', id);
  var val = $(this).find('option').eq(index).attr('data-txt');
  $(this).attr('data-txt', val);
  var areaData = page.addressData[idx1].children[index].children;
  var areaContent = '';
  for (var i = 0; i < page.addressData[idx1].children[index].children.length; i++) {
    areaContent += "<option data-val='" + areaData[i].value + "'data-txt='" + areaData[i].text + "' value='" + areaData[i].text + "' >" + areaData[i].text + "</option>";
  }
  $(this).closest('.address_sel').find('#areatpl').html(areaContent);
});

/*调用区列表*/
body.on('change', '#areatpl', function() {
  var index = this.selectedIndex;
  var idx1 = $(this).closest('.address_sel').find('.province').attr('data-id');
  var val1 = $(this).closest('.address_sel').find('.province').attr('data-txt');
  var idx2 = $(this).closest('.address_sel').find('.city').attr('data-id');
  var val2 = $(this).closest('.address_sel').find('.city').attr('data-txt');

  var id = $(this).find('option').eq(index).attr('data-val');
  $(this).attr('data-id', id);
  var val = $(this).find('option').eq(index).attr('data-txt');
  $(this).attr('data-txt', val);

  $('.user_add').val(idx1 + ' ' + idx2 + ' ' + id).attr('data-v', val1 + ' ' + val2 + ' ' + val);

  if ($(this).closest('.am-tab-panel').hasClass('pickUpTab')) {
    setStorePicker(id);
  }

});

/*获取全部地址*/
getJson(Domain + '/common/doLoadAllArea.do', {}, function(data) {
  console.log(data);
  var setTime = new Date();
  page.addressData = data.data;
  //加载省选项
  var provinceContent = "";
  for (var i = 0; i < page.addressData.length; i++) {
    provinceContent += "<option data-val='" + page.addressData[i].value + "'data-txt='" + page.addressData[i].text + "' value='" + page.addressData[i].text + "' >" + page.addressData[i].text + "</option>";
  }
  $('#provice').append(provinceContent);
});

